import cTitle from "components/title";
import { yzPay } from "../../../utils/yz_pay"; //引入支付方法
import diyBackGround from "./componet/diyBackGround";
import payKeyboard from "components/payKeyboard";
export default {
  mixins: [yzPay],
  data() {
    return {
      credit2: 0,
      buttons: [],
      typename: "",
      recharge: "",
      ordersn: "",
      money: "",
      activatDes: {},
      priceDifferencePool:false,
      tagBalance:false,

      // 选择的充值方式
      pay_type: "",
      // 余额字样
      balanceLang: this.fun.getBalanceLang(),
      diyname:'',
      isOpenCmc: false, //首次需要验证是否开启cmc
      
      popupSpecs:false,
      plugin_name:'',
      recharge_double:'',
    };
  },

  computed: {
    discountType (num) {
      return num => {
        num = num ? num : 0;
        if (this.activatDes.proportion_status == 0) {
          return num + '元';
        } else {
          return num + '%';
        }
      };
    }
  },
  methods: {
    // 初始化参数
    getData() {
      let url = 'finance.balance.balance';
      if(this.priceDifferencePool){
        url = 'plugin.price-difference-pool.frontend.modules.pool.controllers.recharge.page';
      }else if(this.tagBalance){
        url = "plugin.tag-balance.frontend.recharge.page";
      }else if (this.$route.name == 'YccRecharge'){
        url = "plugin.ycc-money.frontend.recharge.index";
      }
      $http.get(url).then(
        (response)=> {
          if (response.result == 1) {
            if(this.priceDifferencePool){
              let basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
              this.diyname = basic_info.price_difference_pool_name?basic_info.price_difference_pool_name:'资金池'
              this.credit2 = response.data.pool;
              this.buttons = response.data.buttons;
              this.recharge = 1;
              return
            }
            if(this.tagBalance){
              console.log(response.data)
              let basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
              this.diyname = response.data.tag?.tag_name;
              this.credit2 = response.data.balance;
              this.buttons = response.data.button;
              this.typename = '充值';
              this.recharge = 1;
              return
            }
            if(this.$route.name == 'YccRecharge'){
              this.balanceLang = '余额';
              this.credit2 = response.data.plugin_member.amount;
              this.recharge = 1;
              this.buttons = response.data.payment_list;
              this.plugin_name = response.data.plugin_name;
              this.recharge_double = response.data.recharge_double;
              return
            }
              this.credit2 = response.data.credit2;
              this.buttons = response.data.buttons;
              this.typename = response.data.typename;
              this.recharge = response.data.recharge;
              this.activatDes = response.data.remark;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        });
    },
    async priceDifferencePool_confirm(type){
      let json ={
        pay_id:type,
        amount:this.money
      }
      let url = 'plugin.price-difference-pool.frontend.modules.pool.controllers.recharge.apply';
      
      await $http.get(url, json).then(
        response=>{
          if(response.result == 1){
            this.rechargePay(type,response,response.data.order_sn,'price-difference-pool')
          }else{
            this.$dialog.alert({ message: response.msg });
          }
        }
      )
    },
    // YCC资产插件使用商城支付类型需要order_pay_id,后端定的流程
    getMergaPay(ids,btn){
      $http
        .get("order.merge-pay", {order_ids:ids}, "加载中")
        .then(response => {
          if (response.result === 1) {
            this.order_pay_id = response.data.order_pay.id;
            this.uid = response.data.order_pay.uid;
            if(btn.other.need_password){
              this.popupSpecs = true;
              return
            }
            this.confirm(btn);
          } else {
            this.$dialog.alert({message:response.msg});
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getVerifyPassword(pwd){
      $http.get("payment.password.check", { password: pwd }, "").then(
        (response) => {
          if (response.result == 1) {
            // console.log('密码正确') 
            // ycc资产充值 走商城余额支付
            this.balancePay(pwd,'','')
            this.changePopupSpecs();
          } else {
            this.$dialog
              .confirm({ message: response.msg })
              .then(() => {
                if (response.data.code == 2001) {
                  // 商城支付密码设置未开启
                  // this.$router.go(-1)
                } else if (response.data.code == 2002) {
                  // 用户未设置支付密码
                  // 去设置密码
                  this.$router.push(this.fun.getUrl("set_balance_password", {}));
                } else if (response.data.code == 2003) {
                  // 支付密码错误,重新输入
                  this.payarr = "";
                  this.changePopupSpecs();
                }
              })
              .catch(() => {
                if (response.data.code == 2001) {
                  // 商城支付密码设置未开启
                } else if (response.data.code == 2002) {
                  // 用户未设置支付密码
                } else if (response.data.code == 2003) {
                  // 支付密码错误
                }
              });
          }
        });
    },
    // 安全键盘组件
    onInput(e){
      console.log(e,'dddddddddd')
      this.getVerifyPassword(e);
    },
    changePopupSpecs(e){
      this.popupSpecs = false;
    },
    yccConfirm(type,btn){
      console.log(btn.other.need_password,'dddddddd')
      $http.get("plugin.ycc-money.frontend.create.index", {ycc_money_price:this.money}).then(
        response=>{
          if(response.result == 1){
            this.getMergaPay(response.data.order_ids,btn);
          }else{
            this.$dialog.alert({ message: response.msg });
          }
        }
      )
    },
    // 确认充值
    confirm_post(type,btn) {
      if(this.priceDifferencePool){
        this.priceDifferencePool_confirm(type);
        return
      }
      this.pay_type = type;
      if (!this.money || this.money <= 0) {
        this.$dialog.alert({ message: "输入值不能小于0" });
        return;
      }
      if (type == 202006 && !this.isOpenCmc) {
        this.isHaveCmcInfo();
        return;
      }
      this.money = parseFloat(this.money);
      // ycc资产充值
      if(this.$route.name == 'YccRecharge'){
        this.yccConfirm(type,btn);
        return
      }
      let url = "finance.balance.recharge"; // "&client_type=2&app_type=wechat"
      if(this.tagBalance){
        url = 'plugin.tag-balance.frontend.recharge.index'
      }
      if (window.isMiniprogram) {
        url += "&client_type=2&app_type=wechat";
      }
      $http.get(url, { pay_type: this.pay_type, recharge_money: this.money }, "正在充值").then(
        response => {
          if (response.result == 1) {
            if(this.pay_type == '88'){
              this.$dialog
              .alert({ message: "充值成功" })
              .then(()=>{
                this.$router.go(-1)
              });
              
              return
            }
            if (this.fun.isTextEmpty(response.data.ordersn)) {
              this.$dialog.alert({ message: "参数错误" });
              return false;
            }
            this.ordersn = response.data.ordersn;
            let keyOption = this.tagBalance?'tagRecharge':'balance';
            this.rechargePay(this.pay_type, response, this.ordersn, keyOption);
          } else {
            this.$dialog.alert({ message: response.msg });
            if(this.pay_type == 101){
              if(response.data.eplus_bind_mobile == 1){
                // 绑定手机
                this.$dialog.alert({
                  message: response.msg
                }).then(()=>{
                  this.$router.push(this.fun.getUrl("wisdomBind", { option: "phone",to:'pay' }));
                })
              }
            }
          }
        });
    },
    isHaveCmcInfo() {
      $http.get("plugin.cmc-pay.frontend.account.get-cmc-account", {}, "").then(
        (response)=> {
          if (response.result === 1) {
            this.isOpenCmc = true;
            this.confirm_post(202006);
          } else {
            this.$dialog.alert({ message: "没有CMC账号！" });
            this.$router.push(this.fun.getUrl("cmcPayInfo", {}));
          }
        });
    },
    btnclass(type) {
      if (type == 1 || type == 6 || type == 9 || type == 12 || type == 20 || type == 22) {
        return "icon-balance_i";
      } else if (type == 2 || type == 10) {
        return "icon-balance_j";
      } else if (type == 18) {
        return "icon-balance_d";
      } else if (type == 19) {
        return "icon-balance_g";
      } else {
        return "icon-balance_h";
      }
    },
    btnclass_pdp(type){
      if (type == 1 || type == 6 || type == 9 || type == 12 || type == 20 || type == 22 || type == 48 || type == 28 || type == 55 || type == 50) {
        return "icon-pay_wechat";
      } else if (type == 10) {
        return "icon-balance_j";
      } else if(type == 49 || type == 2  || type == 29){
        return "icon-pay_alipay"
      }else if (type == 18) {
        return "icon-balance_d";
      } else if (type == 59) {
        return "icon-pay_quick";
      } else if(type == 3){
        return "icon-pay_yue";
      }
    }
  },
  activated() {
    window.scrollTo(0, 0);
    if(this.$route.name == 'balance_rechargepdp'){
      //price-difference-pool 插件的充值页 复用样式
      this.priceDifferencePool = true;
    }
    if( this.$route.name == "balance_rechargeTag"){
      console.log('tag-balance复用样式')
      this.tagBalance = true;
    }
    console.log(this.$route);
    this.pay_type = "";
    this.money = "";
    if (this.$route.params.num) {
      this.money = this.$route.params.num;
    }
    this.getData(); // 初始化参数
  },
  components: { cTitle, diyBackGround,payKeyboard }
};
